package org.hibiscus.jdp.app.dbdoc.oracle;

import org.hibiscus.jdp.app.dbdoc.AbstractSqlUtils;
import org.hibiscus.jdp.app.dbdoc.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

/**
 * 描述：Oracle工具类
 *
 * @author rannuo
 * @date 2019/3/16
 */
public class OracleUtils extends AbstractSqlUtils {

	private static final Logger log = LoggerFactory.getLogger(OracleUtils.class);

	private static final String URL_PREFIX = "jdbc:oracle:thin:@";
	private static final String DEFAULT_URL_PARAM = "?characterEncoding=utf8&useSSL=utf8&serverTimezone=GMT%2B8";
	private static final String DEFAULT_URL = URL_PREFIX + "127.0.0.1:1521:ORCL" + DEFAULT_URL_PARAM;

	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			log.error("未找到匹配驱动", e);
		}
	}

	private static String buildUrl(String host) {
		String url = DEFAULT_URL;
		if (host != null && !"".equals(host)) {
			url = URL_PREFIX + host + DEFAULT_URL_PARAM;
		}
		return url;
	}

	static List<Map<String, String>> getTableStructureData(DataSource dataSource, String sql, boolean isMetaInfo) {
		return getTableStructureData(buildUrl(dataSource.getHost()), dataSource.getUsername(), dataSource.getPassword(), sql, isMetaInfo);
	}
}
